home *** CD-ROM | disk | FTP | other *** search
- unit LockInfo;
-
- interface
-
- uses SysUtils, DbiTypes, DbiProcs, DbiErrs, DBTables;
-
- type
- TLockType = (lkRecordWrite, lkRecordRead, lkPdoxGroup, lkPdoxImage,
- lkOpen, lkRead, lkWrite, lkExcl, lkError, lkUnknown,
- lkIgnore);
-
- TLockInfoType = (liLockType, liUsername, liNetSess, liLocalSess, liRecNo);
-
- TUserName = string[DBIMAXUSERNAMELEN];
- TLookFor = set of TLockInfoType;
- TScores = set of 1..5;
-
- TLocksList = class(TObject)
- private
- FMaxScore: TScores;
- FCursor: HDBICur;
- FLookFor: TLookFor;
- FLockDesc: LOCKDesc;
- FLockType: TLockType;
- FUserName: TUserName;
- FNetSession: Word;
- FOurSession: Word;
- FRecNumber: LongInt;
- FDoFindFirst: Boolean;
- FTable: TTable;
- procedure OpenLockList;
- procedure SetLockType(const Value: TLockType);
- procedure SetUser(const Value: TUserName);
- function GetUser: TUserName;
- procedure SetNetSess(const Value: Word);
- procedure SetOurSess(const Value: Word);
- procedure SetRecNo(const Value: LongInt);
- procedure SetTable(const Value: TTable);
- procedure SetLookFor(const Value: TLookFor);
- public
- constructor Create;
- destructor Destroy; override;
- procedure SetParams(const LType: TLockType; const LUser: TUserName;
- const LNetSess, LOurSess: Word; const LRecNo: LongInt);
- function FindFirst(var LockInfo: LOCKDesc): Boolean;
- function FindNext(var LockInfo: LOCKDesc): Boolean;
- property Table: TTable read FTable write SetTable;
- property LockType: TLockType read FLockType write SetLockType;
- property UserName: TUserName read GetUser write SetUser;
- property NetSession: Word read FNetSession write SetNetSess;
- property LocalSession: Word read FOurSession write SetOurSess;
- property RecNo: LongInt read FRecNumber write SetRecNo;
- property LookFor: TLookFor read FLookFor write SetLookFor;
- end;
-
- function GetLockUser(ATable: TTable; RecNum: LongInt): TUserName;
-
- implementation
-
- uses DB;
-
- function GetLockUser(ATable: TTable; RecNum: LongInt): TUserName;
- var LckDesc: LOCKDesc;
- LckCur: HDbiCur;
- begin
- Result := '';
- Check(DbiOpenLockList(ATable.Handle, True, False, LckCur));
- Check(DbiSetToBegin(LckCur));
- while (DbiGetNextRecord(LckCur, dbiNOLOCK, @LckDesc, nil) = DBIERR_NONE) do
- if (LckDesc.iRecNum = RecNum) then
- begin
- Result := StrPas(LckDesc.szUserName);
- break;
- end;
- Check(DbiCloseCursor(LckCur));
- end;
-
- constructor TLocksList.Create;
- begin
- FDoFindFirst := True;
- FCursor := nil;
- FUserName := '';
- FLookFor := [liLockType,liUsername,liNetSess,liLocalSess,liRecNo];
- end;
-
- destructor TLocksList.Destroy;
- begin
- if FCursor <> nil then Check(DbiCloseCursor(FCursor));
- end;
-
- procedure TLocksList.OpenLockList;
- begin
- if not FTable.Active then raise Exception.Create('Table is closed');
- if FCursor <> nil then Check(DbiCloseCursor(FCursor));
- FCursor := nil;
- Check(DbiOpenLockList(FTable.Handle, True, True, FCursor));
- Check(DbiSetToBegin(FCursor));
- end;
-
- procedure TLocksList.SetTable(const Value: TTable);
- begin
- if not Value.Active then raise Exception.Create('Table is closed');
- FTable := Value;
- end;
-
- procedure TLocksList.SetLookFor(const Value: TLookFor);
- begin
- FLookFor := Value;
- FDoFindFirst := True;
- end;
-
- procedure TLocksList.SetParams(const LType: TLockType; const LUser: TUserName;
- const LNetSess, LOurSess: Word;
- const LRecNo: LongInt);
- begin
- FUserName := LUser;
- FLockType := LType;
- FNetSession := LNetSess;
- FOurSession := LOurSess;
- FRecNumber := LRecNo;
-
- FDoFindFirst := True;
- end;
-
- function TLocksList.FindFirst(var LockInfo: LockDesc): Boolean;
- var Score, DefScore: TScores;
- RetCode: DBIResult;
- begin
- Result := False;
- FMaxScore := [1,2,3,4,5];
- DefScore := [];
-
- OpenLockList;
- FDoFindFirst := False;
-
- if not(liLockType in FLookFor) then Include(DefScore,1);
- if not(liUsername in FLookFor) then Include(DefScore,2);
- if not(liNetSess in FLookFor) then Include(DefScore,3);
- if not(liLocalSess in FLookFor) then Include(DefScore,4);
- if not(liRecNo in FLookFor) then Include(DefScore,5);
-
- repeat
- with FLockDesc do
- begin
- RetCode := DbiGetNextRecord(FCursor, dbiNOLOCK, @FLockDesc, nil);
- if RetCode <> DBIERR_NONE then break;
- Score := DefScore;
-
- if (liLockType in FLookFor) and (TLockType(iType) = FLockType) then
- Include(Score,1);
- if (liUsername in FLookFor) and
- (CompareText(StrPas(FLockDesc.szUsername), FUserName) = 0)
- then Include(Score,2);
- if (liNetSess in FLookFor) and (FNetSession = iNetSession) then
- Include(Score,3);
- if (liLocalSess in FLookFor) and (FOurSession = iSession) then
- Include(Score,4);
- if (liRecNo in FLookFor) and (FRecNumber = iRecNum) then
- Include(Score,5);
-
- Result := (Score = FMaxScore);
- if Result then break;
- end;
- until (RetCode <> DBIERR_NONE);
- if Result then Move(FLockDesc, LockInfo, sizeof(FLockDesc));
- end;
-
- function TLocksList.FindNext(var LockInfo: LockDesc): Boolean;
- var Score, DefScore: TScores;
- RetCode: DBIResult;
- begin
- Result := False;
- DefScore := [];
- if FDoFindFirst then raise Exception.Create('Invalid method call: FindNext');
-
- if not(liLockType in FLookFor) then Include(DefScore,1);
- if not(liUsername in FLookFor) then Include(DefScore,2);
- if not(liNetSess in FLookFor) then Include(DefScore,3);
- if not(liLocalSess in FLookFor) then Include(DefScore,4);
- if not(liRecNo in FLookFor) then Include(DefScore,5);
-
- repeat
- with FLockDesc do
- begin
- RetCode := DbiGetNextRecord(FCursor, dbiNOLOCK, @FLockDesc, nil);
- if RetCode <> DBIERR_NONE then break;
- Score := DefScore;
-
- if (liLockType in FLookFor) and (TLockType(iType) = FLockType) then
- Include(Score,1);
- if (liUsername in FLookFor) and
- (CompareText(StrPas(FLockDesc.szUsername), FUserName) = 0)
- then Include(Score,2);
- if (liNetSess in FLookFor) and (FNetSession = iNetSession) then
- Include(Score,3);
- if (liLocalSess in FLookFor) and (FOurSession = iSession) then
- Include(Score,4);
- if (liRecNo in FLookFor) and (FRecNumber = iRecNum) then
- Include(Score,5);
-
- Result := (Score = FMaxScore);
- if Result then break;
- end;
- until (RetCode <> DBIERR_NONE);
- if Result then Move(FLockDesc, LockInfo, sizeof(FLockDesc));
- end;
-
- procedure TLocksList.SetLockType(const Value: TLockType);
- begin
- FLockType := Value;
- FDoFindFirst := True;
- end;
-
- procedure TLocksList.SetUser(const Value: TUserName);
- begin
- FUserName := Value;
- FDoFindFirst := True;
- end;
-
- function TLocksList.GetUser: TUserName;
- begin
- Result := FUserName;
- end;
-
- procedure TLocksList.SetNetSess(const Value: Word);
- begin
- FNetSession := Value;
- FDoFindFirst := True;
- end;
-
- procedure TLocksList.SetOurSess(const Value: Word);
- begin
- FOurSession := Value;
- FDoFindFirst := True;
- end;
-
- procedure TLocksList.SetRecNo(const Value: LongInt);
- begin
- FRecNumber := Value;
- FDoFindFirst := True;
- end;
-
- end.
-